﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SMS.Entities.BaseClasses;
using SMS.Entities;
using System.Linq;
using System.Collections.ObjectModel;
using SMS.Controls.TrackingSVC;
using System.Windows.Browser;
namespace SMS.ViewModel.Tracking
{
    public class TrackingPageViewModel : BaseNotify
    {
        SMS.Model.TrackingPageModel trackingModel;
        public TrackingPageViewModel()
        {
            SearchCommand = new SMS.Common.Silverlight.DelegateCommand(Search);
            InitializeModel();
            
        }

       #region "Method"

        private void InitializeModel()
        {
            trackingModel = new Model.TrackingPageModel();
            trackingModel.SaleDetailListCompleted += new Action<ObservableCollection<ClsSaleDetail>>(trackingModel_SaleDetailListCompleted);
            trackingModel.SaleHistoryListCompleted += new Action<ObservableCollection<SaleHistory>>(trackingModel_SaleHistoryListCompleted);
        }


        void trackingModel_SaleDetailListCompleted(ObservableCollection<ClsSaleDetail> obj)
        {
            if (obj != null)
            {
                int i=0;
                SalesDetailList = obj;
                SalesDetailList.ToList().ForEach(m => m.SerialNo = ++i);                
            }
        }

        void trackingModel_SaleHistoryListCompleted(ObservableCollection<SaleHistory> obj)
        {
            if (obj != null && obj.Count > 0)
            {
                IsVisible = Visibility.Visible;
                SalesHistoryList = obj;
                SelectedSaleHistory = SalesHistoryList.FirstOrDefault();
                if (SelectedSaleHistory != null)
                    trackingModel.GetSalesDetail(App.LoggedinUserName, SelectedSaleHistory.Sale_ID);
                UserMessage = string.Empty;
            }
            else
            {
                IsVisible = Visibility.Collapsed;
                UserMessage = "Record Not Found!!";
            }
        }

        private void Search(object param)
        {
            trackingModel.GetSalesHistory(App.LoggedinUserName, Convert.ToInt32(ReceiptNo));
        }
        #endregion

        #region "Property"

        private SaleHistory _SelectedSaleHistory;

        public SaleHistory SelectedSaleHistory
        {
            get { return _SelectedSaleHistory; }
            set
            {
                _SelectedSaleHistory = value; OnPropertyChanged("SelectedSaleHistory");
                if (value != null)
                    trackingModel.GetSalesDetail(App.LoggedinUserName, value.Sale_ID);
            }
        }

        private ClsSaleDetail _SelectedSaleDetail;

        public ClsSaleDetail SelectedSaleDetail
        {
            get { return _SelectedSaleDetail; }
            set { _SelectedSaleDetail = value; OnPropertyChanged("SelectedSaleDetail"); }
        }

        private ObservableCollection<SaleHistory> _SalesHistoryList;

        public ObservableCollection<SaleHistory> SalesHistoryList
        {
            get { return _SalesHistoryList; }
            set { _SalesHistoryList = value; OnPropertyChanged("SalesHistoryList"); }
        }

        private ObservableCollection<ClsSaleDetail> _SalesDetailList;

        public ObservableCollection<ClsSaleDetail> SalesDetailList
        {
            get { return _SalesDetailList; }
            set { _SalesDetailList = value; OnPropertyChanged("SalesDetailList"); }
        }

        private int _ReceiptNo;

        public int ReceiptNo
        {
            get { return _ReceiptNo; }
            set { _ReceiptNo = value; OnPropertyChanged("ReceiptNo"); }
        }

        private Visibility _IsVisible=Visibility.Collapsed;

        public Visibility IsVisible
        {
            get { return _IsVisible; }
            set { _IsVisible = value; OnPropertyChanged("IsVisible"); }
        }
        
        
        public ICommand SearchCommand { get; set; }

        #endregion
    }
}
